import { useMutation, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { updateSetting } from '../../services/apiSettings.js'

export function useUpdateSettings() {
	const queryClient = useQueryClient()

	const { mutate: updateSettings, isPending: isUpdating } = useMutation({
		mutationFn: (settings) => updateSetting(settings),
		onSuccess: () => {
			toast.success('Settings updated successfully')
			queryClient.invalidateQueries({ queryKey: ['settings'] })
		},
		onError: (err) => {
			toast.error(err.message)
		}
	})

	return { updateSettings, isUpdating }
}
